package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.GraphInfo;
import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.entity.LeaseTerm;
import com.atguigu.lease.model.entity.PaymentType;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.ApartmentInfoMapper;
import com.atguigu.lease.web.app.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.agreement.AgreementDetailVo;
import com.atguigu.lease.web.app.vo.agreement.AgreementItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {

    @Autowired
    private LeaseAgreementMapper leaseAgreementMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private RoomInfoService roomInfoService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private LeaseTermService leaseTermService;

    @Override
    // 获取个人租约基本信息列表
    public List<AgreementItemVo> listItem(Long userId) {

        return leaseAgreementMapper.listItem(userId);
    }

    @Override
    public void updateStatusById(Long id, LeaseStatus leaseStatus) {
        leaseAgreementMapper.updateStatusById(id, leaseStatus);
    }

    @Override
    public AgreementDetailVo getDetailById(Long id) {
        //1.根据id查询租约基本信息
        LeaseAgreement leaseAgreement = leaseAgreementMapper.selectById(id);

        //2.获取公寓id和房间id
        Long apartmentId = leaseAgreement.getApartmentId();
        Long roomId = leaseAgreement.getRoomId();

        //3.根据公寓id获取公寓名称和图片列表
        String apartmentName = apartmentInfoService.getById(apartmentId).getName();
        List<GraphInfo> graphInfos = listGraphInfo(apartmentId, ItemType.APARTMENT);
        List<GraphVo> apartmentGraphVoList = new ArrayList<>();
        for (GraphInfo graphInfo : graphInfos) {
            GraphVo graphVo = new GraphVo();
            graphVo.setName(graphInfo.getName());
            graphVo.setUrl(graphInfo.getUrl());
            apartmentGraphVoList.add(graphVo);
        }

        //4.根据房间id获取房间号和图片列表
        String roomNumber = roomInfoService.getById(roomId).getRoomNumber();
        List<GraphInfo> graphInfos2 = listGraphInfo(apartmentId, ItemType.ROOM);
        List<GraphVo> roomGraphVoList = new ArrayList<>();
        for (GraphInfo graphInfo : graphInfos2) {
            GraphVo graphVo = new GraphVo();
            graphVo.setName(graphInfo.getName());
            graphVo.setUrl(graphInfo.getUrl());
            roomGraphVoList.add(graphVo);
        }

        //5.根据租约id获取支付方式
        PaymentType paymentType = paymentTypeService.getById(leaseAgreement.getPaymentTypeId());
        String paymentTypeName = paymentType.getName();

        //6.根据租约id获取租期
        LeaseTerm leaseTerm = leaseTermService.getById(leaseAgreement.getLeaseTermId());
        Integer leaseTermMonthCount = leaseTerm.getMonthCount();
        String leaseTermUnit = leaseTerm.getUnit();

        //7.封装数据
        AgreementDetailVo agreementDetailVo = new AgreementDetailVo();
        BeanUtils.copyProperties(leaseAgreement, agreementDetailVo);
        agreementDetailVo.setApartmentName(apartmentName);
        agreementDetailVo.setApartmentGraphVoList(apartmentGraphVoList);
        agreementDetailVo.setRoomNumber(roomNumber);
        agreementDetailVo.setRoomGraphVoList(roomGraphVoList);
        agreementDetailVo.setPaymentTypeName(paymentTypeName);
        agreementDetailVo.setLeaseTermMonthCount(leaseTermMonthCount);
        agreementDetailVo.setLeaseTermUnit(leaseTermUnit);
        return agreementDetailVo;
    }

    //获取图片列表方法
    public List<GraphInfo> listGraphInfo(Long itemId, ItemType itemType) {
        LambdaQueryWrapper<GraphInfo> graphWrapper = new LambdaQueryWrapper<>();
        graphWrapper.eq(GraphInfo::getItemId, itemId);
        graphWrapper.eq(GraphInfo::getItemType, itemType);
        List<GraphInfo> apartmentGraphList = graphInfoService.list(graphWrapper);
        return apartmentGraphList;
    }
}




